//
//  EF_WKWebView.m
//  J智现场
//
//  Created by song on 2017/11/13.
//  Copyright © 2017年 王长飞. All rights reserved.
//

#import "ProductDetailController.h"
#import <WebKit/WebKit.h>
#import "ProductDetailModel.h"
#import "WKWebViewController.h"

@interface ProductDetailController ()<BaseModelProtocol>

@property(nonatomic,strong)ProductDetailModel *productModel;
@property(nonatomic,strong)UIImageView *productImageView;
@property(nonatomic,strong)NSString *thirdUrl;

@end

@implementation ProductDetailController

- (void)initView
{
    [self.navBarView addTitle:@"产品详情"];
    [super initView];
    self.view.backgroundColor = [UIColor whiteColor];

    [self.productModel getProductDetailDataWith:self.secondMenu];
    
}

- (void)getDataFinish:(BaseModel *)baseModel netResponse:(BaseResponse *)response{
    ProductDetailResponse *resp = (ProductDetailResponse *)response;
    ProductDetailData *data = resp.resObject;
    [self createUIWith:data];
}

- (void)createUIWith:(ProductDetailData *)data{
    NSDictionary *dic = [data toDictionary];
    [self addProfuctImageView:data.imageUrl];
    if(data.appProductCommissionDTO != nil && data.appProductCommissionDTO != NULL && [dic[@"appProductCommissionDTO"] isKindOfClass:[NSDictionary class]]){
        ProductDetailDTO *dto = (ProductDetailDTO*)data.appProductCommissionDTO;
        NSArray<ProductDetailPercent> *percentList = dto.percentList;
        NSMutableArray *totalList = [NSMutableArray array];
        if(percentList.count > 0){
            ProductDetailPercent *firstPercent = percentList.firstObject;
            NSMutableArray *titleArray = [NSMutableArray arrayWithObject:@"缴费期/佣金"];
            for (int i = 0; i<firstPercent.percentMap.allKeys.count; i++) {
                [titleArray addObject:[NSString stringWithFormat:@"第%d年",i+1]];
            }
            [totalList addObject:titleArray];
            for (int i = 0; i<percentList.count; i++) {
                NSMutableArray *percentArray = [NSMutableArray array];
                ProductDetailPercent *percent = percentList[i];
                [percentArray addObject:percent.paymentPeriod];
                NSDictionary *percentMap = percent.percentMap;
                NSArray *values = percentMap.allValues;
                [percentArray addObjectsFromArray:values];
                [totalList addObject:percentArray];
            }
        }

        [self initTable:totalList];
    }
    [self addBottomView];
    self.thirdUrl = data.productDetailThirdUrl;

}

- (void)addProfuctImageView:(NSString *)imageUrl{
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, KNavFullHeight+15, SCREEN_WIDTH-30, 170)];
    _productImageView = imageView;
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:kDefaultImage];
    [self.view addSubview:imageView];
}

- (void)initTable:(NSMutableArray *)totalList{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, _productImageView.bottom+30, SCREEN_WIDTH-20, 300)];
    [self.view addSubview:scrollView];
    
    for (int i = 0; i < totalList.count; i++) {
        NSArray *array = totalList[i];
        for (int k = 0; k < array.count; k++) {
            float width = k==0?90:46;
            float left = k==0?0:(90+(k-1)*46);
            float height = i==0?40:30;
            float top = i == 0?0:(i-1)*30+40;
            UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(left, top, width, height)];
            titleLab.text = array[k];
            titleLab.textColor = kTitleColor_midden;
            titleLab.font = KFontSmall;
            titleLab.textAlignment = NSTextAlignmentCenter;
            [titleLab setViewEdgeWithBorderWidth:0.5 cornerRadius:0 borderColor:kColorLine];
            [scrollView addSubview:titleLab];
        }
    }
}

- (void)addBottomView{
    UILabel *detailLab = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50 , SCREEN_WIDTH, 50)];
    detailLab.text = @"查看详情";
    detailLab.textColor = [UIColor whiteColor];
    detailLab.font = kFontMidden;
    detailLab.textAlignment = NSTextAlignmentCenter;
    detailLab.backgroundColor = [UIColor orangeColor];
    [detailLab addTapCallBack:self sel:@selector(detailLabTap)];
    [self.view addSubview:detailLab];
}

- (void)detailLabTap
{
    WKWebViewController *webController = [[WKWebViewController alloc]init];
    webController.titleString = @"详情";
    webController.urlString = self.thirdUrl;
    [self.navigationController pushViewController:webController animated:YES];
}

- (ProductDetailModel *)productModel{
    
    if (nil == _productModel) {
        _productModel = [[ProductDetailModel alloc]initWithDelegate:self cache:NO];
    }
    return _productModel;
}

@end
